package wit.dm.t2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    //借助一个HashMap保存合法的用户名和密码
    HashMap<String, String> hashMap = new HashMap<String, String>(){{
        put("yj", "123456");
        put("hyt", "456789");
    }};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1 = (EditText) findViewById(R.id.editText1);
                EditText editText2 = (EditText) findViewById(R.id.editText2);
                String username = editText1.getText().toString();
                String password = editText2.getText().toString();
                if (!hashMap.containsKey(username)) {//用户名不在哈希表中
                    Toast.makeText(MainActivity.this, "您输入的用户名不存在", Toast.LENGTH_SHORT).show();
                } else if (!hashMap.get(username).equals(password)) {//用户名与密码不匹配
                    Toast.makeText(MainActivity.this, "您输入的密码与用户名不匹配", Toast.LENGTH_SHORT).show();
                } else {//binggo!
                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                    intent.putExtra("username", username);
                    startActivity(intent);
                }
            }
        });
    }
}